/* $RCSFile: LanguageSwitcher.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/LanguageSwitcher/LanguageSwitcher.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:21  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:55  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Collections;
using System.Configuration;
using System.Diagnostics;
using System.Text;
using System.Web.Caching;
using System.IO;
using System.Threading;
using System.Globalization;

using AddonNice.Settings;
using AddonNice.Core;
using AddonNice.UI;
using AddonNice.UI.DataTypes;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.Diagnostics;
using AddonNice.Helpers;


using Trace2    =   System.Diagnostics.Trace;
using Path2     =   AddonNice.Settings.Path;

namespace AddonNice.PageModules.LanguageSwitcher
{
        public class swstr
        {
            
			public const string LANGSWITCHTYPE_DROPDOWNLIST         =   "LANGSWITCHTYPE_DROPDOWNLIST";
			public const string LANGSWITCHTYPE_LINKS                =   "LANGSWITCHTYPE_LINKS";
			public const string LANGSWITCHTYPE_LINKSHORIZONTAL      =   "LANGSWITCHTYPE_LINKSHORIZONTAL";
			public const string LANGUAGESWITCHER_TYPES              =   "LANGUAGESWITCHER_TYPES";

            // Language Switcher Module - DisplayOptions
			public const string LANGSWITCHTDISPLAY_CULTURELIST      =   "LANGSWITCHTDISPLAY_CULTURELIST";
			public const string LANGSWITCHTDISPLAY_UICULTURELIST    =   "LANGSWITCHTDISPLAY_UICULTURELIST";
			
            
            public const string LANGSWITCHTDISPLAY_INSIDE           =   "LANGSWITCHTDISPLAY_INSIDE";
            public const string LANGSWITCHTDISPLAY_OUTSIDE          =   "LANGSWITCHTDISPLAY_OUTSIDE";
            public const string LANGSWITCHTDISPLAY_INSIDEOUTSIDE    =   "LANGSWITCHTDISPLAY_INSIDEOUTSIDE";
            public const string LANGSWITCHTDISPLAY_NONE             =   "LANGSWITCHTDISPLAY_NONE";
			// Flags
			public const string LANGUAGESWITCHER_FLAGS              =   "LANGUAGESWITCHER_FLAGS";
			public const string LANGUAGESWITCHER_FLAGTYPE           =   "LANGUAGESWITCHER_FLAGTYPE";
			// Labels
			public const string LANGUAGESWITCHER_DISPLAYLABELS      =   "LANGUAGESWITCHER_DISPLAYLABELS";
			public const string LANGUAGESWITCHER_LABELS             =   "LANGUAGESWITCHER_LABELS";
			// Language Switcher Module - NamesOptions
			public const string LANGSWITCHTNAMES_NATIVENAME         =   "LANGSWITCHTNAMES_NATIVENAME";
			public const string LANGSWITCHTNAMES_ENGLISHNAME        =   "LANGSWITCHTNAMES_ENGLISHNAME";
			public const string LANGSWITCHTNAMES_DISPLAYNAME        =   "LANGSWITCHTNAMES_DISPLAYNAME";
			// Names
			public const string LANGUAGESWITCHER_NAMES              =   "LANGUAGESWITCHER_NAMES";
			// Use flag images from portal's images folder?
			public const string LANGUAGESWITCHER_CUSTOMFLAGS        =   "LANGUAGESWITCHER_CUSTOMFLAGS";
        }

    /// <summary>
    /// Initializer class for LanguageSwitcher 
    /// </summary>
    public class LanguageSwitcherSI : PortalModuleControlSI
    {
        public LanguageSwitcherSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]LanguageSwitcherSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]LanguageSwitcherSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]LanguageSwitcherSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

            string extgrp                               =   pm.ExtendedResourcesGroup;

            // Language Switcher Module - Type
			ArrayList languageSwitcherTypesOptions      =   new ArrayList();
			languageSwitcherTypesOptions.Add(new SettingOption((int)LangChooserType.DropDownList, LocalizeHelper.Localize(swstr.LANGSWITCHTYPE_DROPDOWNLIST, "DropDownList")));
			languageSwitcherTypesOptions.Add(new SettingOption((int)LangChooserType.VerticalLinksList, LocalizeHelper.Localize(swstr.LANGSWITCHTYPE_LINKS, "Links")));
			languageSwitcherTypesOptions.Add(new SettingOption((int)LangChooserType.HorizontalLinksList, LocalizeHelper.Localize(swstr.LANGSWITCHTYPE_LINKSHORIZONTAL, "Links Horizontal")));

			SettingItemSpecial languageSwitchType       =   new SettingItemSpecial(swstr.LANGUAGESWITCHER_TYPES,new CustomListDataType(languageSwitcherTypesOptions, "Name", "Val"));
			languageSwitchType.EnglishName              =   "Language Switcher Type";
			languageSwitchType.Description              =   "How your language switcher should look like.";
			languageSwitchType.Value                    =   ((int)LangChooserType.VerticalLinksList).ToString();
			languageSwitchType.Order                    =   10;
			languageSwitchType.ExtendedGroupName        =    extgrp;
			bSettings.AddSetting(languageSwitchType);

			// Language Switcher Module - DisplayOptions
			ArrayList languageSwitcherDisplayOptions    =   new ArrayList();
			languageSwitcherDisplayOptions.Add(new SettingOption((int)LangChooserDisplay.DisplayInside, LocalizeHelper.Localize(swstr.LANGSWITCHTDISPLAY_INSIDE, "Inside")));
			languageSwitcherDisplayOptions.Add(new SettingOption((int)LangChooserDisplay.DisplayOutSide, LocalizeHelper.Localize(swstr.LANGSWITCHTDISPLAY_OUTSIDE, "Outside")));
			languageSwitcherDisplayOptions.Add(new SettingOption((int)(LangChooserDisplay.DisplayOutSide|LangChooserDisplay.DisplayInside), LocalizeHelper.Localize(swstr.LANGSWITCHTDISPLAY_INSIDEOUTSIDE, "Inside and Outside")));
			languageSwitcherDisplayOptions.Add(new SettingOption((int)LangChooserDisplay.DisplayNone, LocalizeHelper.Localize(swstr.LANGSWITCHTDISPLAY_NONE, "None")));

			// Flags display and position
			SettingItemSpecial languageSwitchFlags      =   new SettingItemSpecial(swstr.LANGUAGESWITCHER_FLAGS,new CustomListDataType(languageSwitcherDisplayOptions, "Name", "Val"));
			languageSwitchFlags.EnglishName             =   "Flags Display";
			languageSwitchFlags.Description             =   "Select here how flags should be displayed.";
			languageSwitchFlags.Value                   =   ((int)LangChooserDisplay.DisplayOutSide).ToString();
			languageSwitchFlags.Order                   =   20;
			languageSwitchFlags.ExtendedGroupName       =   extgrp;
			bSettings.AddSetting(languageSwitchFlags);

            ArrayList languageSwitcherFlagType          =   new ArrayList();
			languageSwitcherFlagType.Add(new SettingOption((int)LangChooserFlagType.DisplayCultureList, LocalizeHelper.Localize(swstr.LANGSWITCHTDISPLAY_CULTURELIST, "Using Culture Name")));
			languageSwitcherFlagType.Add(new SettingOption((int)LangChooserFlagType.DisplayUICultureList, LocalizeHelper.Localize(swstr.LANGSWITCHTDISPLAY_UICULTURELIST, "Using UI Culture Name")));

			// Flags Type
			SettingItemSpecial languageSwitchFlagsType  =   new SettingItemSpecial(swstr.LANGUAGESWITCHER_FLAGTYPE,new CustomListDataType(languageSwitcherFlagType, "Name", "Val"));
			languageSwitchFlagsType.EnglishName         =   "Show Flags as";
			languageSwitchFlagsType.Description         =   "Select here how flags should look like.";
			languageSwitchFlagsType.Value               =   ((int)LangChooserFlagType.DisplayCultureList).ToString();
			languageSwitchFlagsType.Order               =   25;
			languageSwitchFlagsType.ExtendedGroupName   =   extgrp;
			bSettings.AddSetting(languageSwitchFlagsType);

			// Labels : Dislay
			SettingItemSpecial languageSwitchDispLabels =   new SettingItemSpecial(swstr.LANGUAGESWITCHER_DISPLAYLABELS,new BooleanDataType());
			languageSwitchDispLabels.EnglishName        =   "Display Labels";
			languageSwitchDispLabels.Description        =   "Uncheck to not displaylabels.";
			languageSwitchDispLabels.Value              =   StStr.strTrue;
			languageSwitchDispLabels.Order              =   28;
			languageSwitchDispLabels.ExtendedGroupName  =   extgrp;
			bSettings.AddSetting(languageSwitchDispLabels);


            // Labels Type
			SettingItemSpecial languageSwitchLabels     =   new SettingItemSpecial(swstr.LANGUAGESWITCHER_LABELS,new CustomListDataType(languageSwitcherFlagType, "Name", "Val"));
			languageSwitchLabels.EnglishName            =   "Show Labels as";
			languageSwitchLabels.Description            =   "Select here how Labels should look like.";
			languageSwitchLabels.Value                  =   ((int)LangChooserFlagType.DisplayCultureList).ToString();
			languageSwitchLabels.Order                  =   30;
			languageSwitchLabels.ExtendedGroupName      =   extgrp;
			bSettings.AddSetting(languageSwitchLabels);

			// Language Switcher Module - NamesOptions
			ArrayList languageSwitcherNamesOptions      =   new ArrayList();
			languageSwitcherNamesOptions.Add(new SettingOption((int)LangChooserName.NativeName, LocalizeHelper.Localize(swstr.LANGSWITCHTNAMES_NATIVENAME, "Native Name")));
			languageSwitcherNamesOptions.Add(new SettingOption((int)LangChooserName.EnglishName, LocalizeHelper.Localize(swstr.LANGSWITCHTNAMES_ENGLISHNAME, "English Name")));
			languageSwitcherNamesOptions.Add(new SettingOption((int)LangChooserName.DisplayName, LocalizeHelper.Localize(swstr.LANGSWITCHTNAMES_DISPLAYNAME, "Display Name")));

			// Names
			SettingItemSpecial languageSwitcherName     =   new SettingItemSpecial(swstr.LANGUAGESWITCHER_NAMES,new CustomListDataType(languageSwitcherNamesOptions, "Name", "Val"));
			languageSwitcherName.EnglishName            =   "Show names as";
			languageSwitcherName.Description            =   "Select here how names should look like.";
			languageSwitcherName.Value                  =   ((int)LangChooserName.NativeName).ToString();
			languageSwitcherName.Order                  =   40;
			languageSwitcherName.ExtendedGroupName      =   extgrp;
			bSettings.AddSetting(languageSwitcherName);

			// Use flag images from portal's images folder?
			SettingItemSpecial customFlags              =   new SettingItemSpecial(swstr.LANGUAGESWITCHER_CUSTOMFLAGS,new BooleanDataType());
			customFlags.Order                           =   50;
			customFlags.ExtendedGroupName               =   extgrp;
			customFlags.EnglishName                     =   "Use custom flags?";
			customFlags.Description = "Check this if you want to use custom flags from portal's images folder. Custom flags are located in portal folder. /images/flags/";
			customFlags.Value                           =   StStr.strFalse;
			bSettings.AddSetting(customFlags);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]LanguageSwitcherSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
    }

    /// <summary>
	///	Module used to switch portal between different languages 
	/// </summary>
	public partial class LanguageSwitcher : PortalModuleControl
	{
		//public const string LANGUAGE_DEFAULT = "en-US";
		
        protected LangChooser LangChooser1;
			
		public override Guid GuidID 
		{
			get
			{
				return new Guid(StStr.LanguageSwitcherGuid);
			}
		}

		/// <summary>
		/// Admin Module
		/// </summary>
		public override bool AdminModule
		{
			get
			{
				return true;
			}
		}

   		/// <summary>
		/// CTor
		/// </summary>
		public LanguageSwitcher()
		{
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]LanguageSwitcher Ctor Called.",ModuleTraceSwitch.Sw.Info);
		}

        public override string ExtendedResourcesGroup 
		{
            get
            {
                const string extgrp                         =   "LANGUAGESWITCHER";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new LanguageSwitcherSI(bSettings,this);
        }

        protected void Page_Load(object sender, System.EventArgs e)
		{
           if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LanguageSwitcher Page_Load BEG IsPostBack: {0}, IsInAsyncPostBack:{1}, Culture: {2}, UICulture: {3}.",
                        new object[]{IsPostBack,IsInAsyncPostBack,Page.Culture,Page.UICulture}),ModuleTraceSwitch.Sw.Info);
			if (!IsPostBack && !Page.IsCallback )
			{
				LangChooser1.LanguageListString         =   GetLanguageCultureList().ToString();
				LangChooser1.ChangeLanguageAction       =   LangChooserAction.LinkRedirect;
                if ( Settings.ContainsKey(swstr.LANGUAGESWITCHER_TYPES) )
				    LangChooser1.Type                   =   (LangChooserType) Enum.Parse(typeof(LangChooserType), Settings[swstr.LANGUAGESWITCHER_TYPES].Value);
                if (Settings.ContainsKey(swstr.LANGUAGESWITCHER_FLAGS) )
				    LangChooser1.Flags                  =   (LangChooserDisplay) Enum.Parse(typeof(LangChooserDisplay), Settings[swstr.LANGUAGESWITCHER_FLAGS].Value);
                if (Settings.ContainsKey(swstr.LANGUAGESWITCHER_FLAGTYPE) )
				    LangChooser1.FlagType               =   (LangChooserFlagType) Enum.Parse(typeof(LangChooserFlagType), Settings[swstr.LANGUAGESWITCHER_FLAGTYPE].Value);
                
                LangChooser1.DisplayLabels              =   Settings[swstr.LANGUAGESWITCHER_DISPLAYLABELS].ToBool();
				if ( Settings.ContainsKey(swstr.LANGUAGESWITCHER_LABELS) )
                    LangChooser1.Labels                 =   (LangChooserFlagType) Enum.Parse(typeof(LangChooserFlagType), Settings[swstr.LANGUAGESWITCHER_LABELS].Value);
                if ( Settings.ContainsKey(swstr.LANGUAGESWITCHER_NAMES) )
                    LangChooser1.ShowNameAs             =   (LangChooserName) Enum.Parse(typeof(LangChooserName), Settings[swstr.LANGUAGESWITCHER_NAMES].Value);

				if ( Settings.ContainsKey(swstr.LANGUAGESWITCHER_CUSTOMFLAGS) && Settings[swstr.LANGUAGESWITCHER_CUSTOMFLAGS].ToBool() )
					LangChooser1.ImagePath              =   portalSettings.PortalFullPath + "/images/flags/";
				else
					LangChooser1.ImagePath              =   Path2.ToAbsolute(Config.PortalFlagsPath);
			}
          if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]LanguageSwitcher Page_Load END.",ModuleTraceSwitch.Sw.Info);
		}

        private void LangChooser1_ChangeLanguage(object sender, LangChooserEventArgs e)
		{
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LanguageSwitcher LangChooser1_ChangeLanguage BEG EnglishName: {0}.",e.CultureItem.Culture.EnglishName),ModuleTraceSwitch.Sw.Info);
			CultureInfo	c	=	e.CultureItem.UICulture;
			if ( !c.IsNeutralCulture )
				c		    =	c.Parent;
			string url	    =	HttpUrlBuilder.BuildUrl();
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LanguageSwitcher LangChooser1_ChangeLanguage END url: {0}.",url),ModuleTraceSwitch.Sw.Info);
            portalSettings.ReloadPortal(false);
		}

		#region Std Overrides

        override protected void OnInit(EventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LanguageSwitcher OnInit BEG IsPostBack: {0}, IsInAsyncPostBack:{1}.", IsPostBack, IsInAsyncPostBack), ModuleTraceSwitch.Sw.Info);

            InitializeComponent();
            base.OnInit(e);
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]LanguageSwitcher OnInit END.", ModuleTraceSwitch.Sw.Info);
        }
		
		private void InitializeComponent()
		{
			Load                        +=  new EventHandler(Page_Load);
            LangChooser1                =   new LangChooser();
            Controls.Add(LangChooser1);
			LangChooser1.ChangeLanguage	+=  new LangChooserEventHandler(LangChooser1_ChangeLanguage);
		}

        #endregion

    }
}